var Url = {
    getViewUrl: function(view) {
        view = view.replaceAll('/', '.');
        return '/views/' + view;
    }
};

String.prototype.isEmpty = function() {
    if(this.replace(/\s/g,'')=='')
        return true;
    else return false;
}

String.prototype.stripTags = function() {
    return this.replace(/<\/?[^>]+>/g, '');
};

String.prototype.contains = function(string) {
    if(this.indexOf(string) == -1)
        return false;
    else return true;
}

String.prototype.replaceAll = function( token, newToken, ignoreCase ) {
    var _token;
    var str = this + "";
    var i = -1;

    if ( typeof token === "string" ) {

        if ( ignoreCase ) {

            _token = token.toLowerCase();

            while( (
                i = str.toLowerCase().indexOf(
                    token, i >= 0 ? i + newToken.length : 0
                ) ) !== -1
                ) {
                str = str.substring( 0, i ) +
                    newToken +
                    str.substring( i + token.length );
            }
        } else {
            return this.split( token ).join( newToken );
        }

    }
    return str;
};

String.prototype.pluralize = function (count, plural) {
    if (!plural) plural = this + 's';
    return (count == 1 ? this : plural);
};

Number.prototype.numberFormat = function(decimals, dec_point, thousands_sep) {
    var n = this, prec = decimals;

    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };

    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
            _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

Number.prototype.nFormatter = function() {
    var num = this;
    if (num >= 1000000000)
        return (num / 1000000000).toFixed(0) + 'G';
    if (num >= 1000000)
        return (num / 1000000).toFixed(0) + 'M';
    if (num >= 1000)
        return (num / 1000).toFixed(0) + 'K';
    return num;
};

Number.prototype.plusFormatter = function(isFront) {
    var num = this;
    if (num > 99)
        if(isFront)
            return '+99';
        else return '99+';
    return num;
};

Math.generateId = function () {
    var i = Math.random() * 10000000000000000000;
    return i.toString(36);
};

Math.generateGUID = function() {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x7|0x8)).toString(16);
    });
    return uuid;
};

Date.prototype.timeago = function (unix_timestamp, bFull) {
    if(!unix_timestamp) unix_timestamp = this.now();
    var difference_in_seconds = (Math.round((new Date()).getTime() / 1000)) - unix_timestamp,
        current_date = new Date(unix_timestamp * 1000), minutes, hours,
        months = new Array(
            'January','February','March','April','May',
            'June','July','August','September','October',
            'November','December');

    if(difference_in_seconds < 60) {
        if(bFull)
            return difference_in_seconds + " sec ago";
        else
            return difference_in_seconds + "s";
    }else if (difference_in_seconds < 60*60) {
        minutes = Math.floor(difference_in_seconds/60);
        if(bFull)
            return minutes + " min ago";
        else
            return minutes + "m";
    }else if (difference_in_seconds > 60*60 && difference_in_seconds < 60*60*2) {
        if(bFull)
            return "1 hour ago";
        else
            return "1h";
    } else if ( difference_in_seconds >= 60*60*2 && difference_in_seconds < 60*60*24) {
        hours = Math.floor(difference_in_seconds/60/60);
        if(bFull)
            return hours + " hours ago";
        else
            return hours + "h";
    }else if ( difference_in_seconds >= 60*60*24 && difference_in_seconds < 60*60*24*2) {
        if(bFull)
            return "1 day ago";
        else
            return "1d";
    }else if ( difference_in_seconds >= 60*60*24*2 && difference_in_seconds < 60*60*24*7) {
        days = Math.floor(difference_in_seconds/60/60/24);
        if(bFull)
            return days + " days ago";
        else
            return days + "d";
    }else if ( difference_in_seconds >= 60*60*24*7 && difference_in_seconds < 60*60*24*14) {
        if(bFull)
            return "1 week ago";
        else
            return "1w";
    }else if (difference_in_seconds >= 60*60*24*14) {
        weeks = Math.floor(difference_in_seconds/60/60/24/7);
        if(bFull)
            return weeks + " weeks ago";
        else
            return weeks + "w";
    }
    return difference_in_seconds;
}

function l(message) {
    console.log(message);
}